package com.cch.examSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cch.examSystem.entity.Category;
import com.cch.examSystem.entity.Menu_subject;
import com.cch.examSystem.entity.Subject;
import com.cch.examSystem.entity.User;

public class LoginServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		String type=request.getParameter("type");
		String account=request.getParameter("account");
		String pwd=request.getParameter("pwd");
		String student_url="./student/main.jsp";
		String normal_url="./student/main.jsp";
		String manager_url="./student/main.jsp";
		System.out.print(111);
		if(type.equals("student")&&account.equals("111111111111111111")&&pwd.equals("123123123")){
			System.out.print(111);
			out.print("{\"status\":\"success\",\"value\":\""+student_url+"\"}");
		}else{
			out.print("{\"status\":\"error\",\"value\":\"asdfasdfas\"}");
		}
		User user=new User("aaaa",12);
		List subjects=new ArrayList();
		subjects.add(new Category(1,"语文",0));
		subjects.add(new Category(2,"数学",0));
		subjects.add(new Category(3,"英语",0));
		List knowledges=new ArrayList();
		knowledges.add(new Category(4,"文言文",1));
		knowledges.add(new Category(5,"写作",1));
		knowledges.add(new Category(6,"勾股定理",2));
		knowledges.add(new Category(7,"一元二次",2));
		knowledges.add(new Category(8,"文言文",3));
		HttpSession session=request.getSession();
		session.setAttribute("user", user);
		session.setAttribute("subjects", subjects);
		session.setAttribute("knowledges", knowledges);
		out.flush();
		out.close();
	}

}
